package com.xh.bussiness.xkw.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.xh.bussiness.system.bean.request.DictionaryRequest;
import com.xh.bussiness.system.model.CourseSystem;
import com.xh.bussiness.system.model.Dictionary;
import com.xh.bussiness.system.service.ICourseSystemService;
import com.xh.bussiness.system.service.IDictionaryService;
import com.xh.bussiness.tiku.model.BookVersion;
import com.xh.bussiness.tiku.service.IBookVersionService;
import com.xh.bussiness.xkw.constant.XkwAPIURL;
import com.xh.bussiness.xkw.dao.XkwVersionBookMapper;
import com.xh.bussiness.xkw.model.XkwSubject;
import com.xh.bussiness.xkw.model.XkwVersionBook;
import com.xh.bussiness.xkw.service.IXkwSubjectService;
import com.xh.bussiness.xkw.service.IXkwVersionBookService;
import com.xh.bussiness.xkw.utils.ConfigUtils;
import com.xh.bussiness.xkw.utils.XkwHttpsUtils;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class XkwVersionBookServiceImpl extends BaseServiceImpl<XkwVersionBookMapper, XkwVersionBook> implements IXkwVersionBookService {

    private final String XkwApiSeriveUrl;

    @Autowired
    public XkwVersionBookServiceImpl(ConfigUtils configUtils) {
        XkwApiSeriveUrl = configUtils.getXkwApiSeriveUrl();
    }

    @Autowired
    private ConfigUtils configUtils;

    @Autowired
    private IXkwSubjectService xkwSubjectService;
    @Autowired
    private IDictionaryService dictionaryService;
    @Autowired
    private IBookVersionService bookVersionService;
    @Autowired
    private ICourseSystemService courseSystemService;

    /**
     * 保存所有学科网版本教材
     *
     * @throws Exception
     */
    public void sysXkwVersionBook() throws Exception {
        List<XkwVersionBook> versionBookList = new ArrayList<>();
        DictionaryRequest bean = new DictionaryRequest();
        bean.setTypeCode("SUBJECT");
        List<Dictionary> subjects = dictionaryService.queryDictionary(bean);
        for (Dictionary subject : subjects) {
            List<XkwVersionBook> xkwVersion = queryXkwVersion(Integer.parseInt(subject.getCode()));
            for (XkwVersionBook xkwVersionBook : xkwVersion) {
                if (xkwVersionBook.getFlag() == 1) {
                    Dictionary teachBook = new Dictionary();
                    teachBook.setId("teach_book_version_" + xkwVersionBook.getXkwId());
                    teachBook.setName(xkwVersionBook.getName());
                    teachBook.setCode(String.valueOf(xkwVersionBook.getXkwId()));
                    teachBook.setTypeCode("TEACH_BOOK_VERSION");
                    dictionaryService.insert(teachBook);
                    CourseSystem courseSystem = new CourseSystem();
                    courseSystem.setSubjectId(subject.getCode());
                    courseSystem.setSubjectName(subject.getName());
                    courseSystem.setRelationType("TEACH_BOOK_VERSION");
                    courseSystem.setRelationValue(teachBook.getId());
                    courseSystemService.insert(courseSystem);
                } else {
                    BookVersion bookVersion = new BookVersion();
                    bookVersion.setName(xkwVersionBook.getName());
                    bookVersion.setVersionId("teach_book_version_"+xkwVersionBook.getParentId());
                    bookVersion.setSubject(subject.getCode());
                    bookVersion.setId(String.valueOf(xkwVersionBook.getXkwId()));
                    bookVersion.setVersionName(xkwVersionBook.getParentName());
                    bookVersionService.insert(bookVersion);
                }
            }
        }
    }

    /**
     * 查询所有学科网版本教材
     *
     * @throws Exception
     */
    private List<XkwVersionBook> queryXkwVersion(Integer subjectId) {
        List<XkwVersionBook> versionBookList = new ArrayList<>();

        String queryUrl = String.format(XkwAPIURL.GET_BANKS_VERSION_MATERIALS, subjectId);
        String url = XkwApiSeriveUrl + queryUrl;
        String categoriesJson = XkwHttpsUtils.doGet(url);
        List<Map> parse = (List<Map>) JSONArray.parse(categoriesJson);
        parse.forEach(map -> {
            Integer id = Integer.valueOf(map.get("id").toString());
            String name = map.get("name").toString();
            XkwVersionBook xkwVersionBook = new XkwVersionBook(id, subjectId, name, 0, null, 1);
            versionBookList.add(xkwVersionBook);
            String materialsList = map.get("materialsList").toString();
            getbooks(id, name, subjectId, versionBookList, materialsList);

        });
        return versionBookList;
    }

    /**
     * 获取课本节点
     *
     * @param vid             版本ID
     * @param xkwVersionBooks 存储集合
     * @param childJson       子节点Json
     */
    private void getbooks(Integer vid, String vName, Integer subjectId, List<XkwVersionBook> xkwVersionBooks, String childJson) {
        List<Map> parse = (List<Map>) JSONArray.parse(childJson);
        if (!parse.isEmpty()) {
            parse.forEach(map -> {
                Integer id = Integer.valueOf(map.get("id").toString());
                String name = map.get("name").toString();
                XkwVersionBook xkwVersionBook = new XkwVersionBook(id, subjectId, name, vid, vName,2);
                xkwVersionBooks.add(xkwVersionBook);
            });
        }
    }

}
